import random

from flask import Blueprint, jsonify, request

data_api = Blueprint('data_api', __name__)


@data_api.route('/middle_data')
def middle_data_analysis():
    middle_data = {
        "source": {
            "total_information": 9897.56,
            "today_information": 16,
            "total_transaction_money": 1532.88,
            "today_transaction_money": 0.5,
            "total_transaction_area": 2130.26,
            "today_transaction_area": 1.4
        }}
    for key, value in middle_data['source'].items():
        if 'total' in key:
            middle_data['source'][key] = round(random.uniform(1000, 9000), 2)
        else:
            middle_data['source'][key] = round(random.uniform(1, 20), 2)
    return jsonify(middle_data)


@data_api.route('/base_data')
def base_data_analysis():
    base_data = {
        "source": {
            "total_confirmation_area": 2658.98,
            "total_circulation_area": 1980.55,
            "total_expect_area": 987.22
        }}
    for key, value in base_data["source"].items():
        base_data["source"][key] = round(random.uniform(1000, 3000), 2)
    return jsonify(base_data)


@data_api.route('/business_type')
def business_type():
    business_type_data = {"legend_name": ['土地经营权', '土地流转权', '房屋所有权', '集体建设用地', '林权'], "source": [
        {'value': 335, 'name': '土地经营权'},
        {'value': 310, 'name': '土地流转权'},
        {'value': 234, 'name': '房屋所有权'},
        {'value': 135, 'name': '集体建设用地'},
        {'value': 1548, 'name': '林权'}
    ]}
    for item in business_type_data["source"]:
        item["value"] = random.randint(300, 1000)
    return jsonify(business_type_data)


@data_api.route('/land_analysis')
def land_analysis():
    land_analysis_data = {"source": [['product', '本地总面积', '本地已流转面积'],
                                     ['岳阳市', 90, 85.8],
                                     ['益阳市', 83.1, 73.4],
                                     ['长沙市', 86.4, 65.2],
                                     ['株洲市', 72.4, 53.9],
                                     ['衡阳市', 72.4, 53.9],
                                     ['永州市', 72.4, 53.9],
                                     ['娄底市', 72.4, 53.9],
                                     ['郴州市', 72.4, 53.9],
                                     ['湘潭市', 72.4, 53.9]]}
    return jsonify(land_analysis_data)


@data_api.route('/information_delivery')
def information_delivery():
    information_delivery_data = {"source": [['product', '土地流转信息', '找地需求信息'],
                                            ['岳阳市', 90.3, 85.8],
                                            ['益阳市', 83.1, 73.4],
                                            ['长沙市', 86.4, 65.2],
                                            ['株洲市', 72.4, 53.9],
                                            ['衡阳市', 72.4, 53.9],
                                            ['永州市', 72.4, 53.9],
                                            ['娄底市', 10.4, 200.9],
                                            ['郴州市', 72.4, 90.9],
                                            ['湘潭市', 20.4, 300.9], ]}
    return jsonify(information_delivery_data)


@data_api.route('/map_data')
def map_data():
    city = request.args.get("params")
    map_data_ = {
        "source": [
            {
                "name": "长沙",
                "value": 46,
                "middle_data": {
                    "total_information": 189,
                    "today_information": 2,
                    "total_transaction_money": 35.6,
                    "today_transaction_money": 0.1,
                    "total_transaction_area": 89.56,
                    "today_transaction_area": 0.2
                }
            },
            {
                "name": "株洲",
                "value": 81,
                "middle_data": {
                    "total_information": 234,
                    "today_information": 3,
                    "total_transaction_money": 56.66,
                    "today_transaction_money": 0.2,
                    "total_transaction_area": 100.26,
                    "today_transaction_area": 0.3
                }
            },
            {
                "name": "湘潭",
                "value": 94,
                "middle_data": {
                    "total_information": 122.32,
                    "today_information": 1,
                    "total_transaction_money": 45.6,
                    "today_transaction_money": 0.23,
                    "total_transaction_area": 39.06,
                    "today_transaction_area": 0.5
                }
            },
            {
                "name": "衡阳",
                "value": 40,
                "middle_data": {
                    "total_information": 201,
                    "today_information": 5,
                    "total_transaction_money": 44,
                    "today_transaction_money": 0.4,
                    "total_transaction_area": 77.56,
                    "today_transaction_area": 0.54
                }
            },
            {
                "name": "邵阳",
                "value": 67,
                "middle_data": {
                    "total_information": 144.23,
                    "today_information": 6,
                    "total_transaction_money": 77,
                    "today_transaction_money": 0.3,
                    "total_transaction_area": 56,
                    "today_transaction_area": 0.6
                }
            },
            {
                "name": "岳阳",
                "value": 38,
                "middle_data": {
                    "total_information": 32,
                    "today_information": 5,
                    "total_transaction_money": 23,
                    "today_transaction_money": 0.23,
                    "total_transaction_area": 34.56,
                    "today_transaction_area": 0.12
                }
            },
            {
                "name": "常德",
                "value": 50,
                "middle_data": {
                    "total_information": 156.5,
                    "today_information": 8,
                    "total_transaction_money": 66.66,
                    "today_transaction_money": 0.12,
                    "total_transaction_area": 88.88,
                    "today_transaction_area": 0.2
                }
            },
            {
                "name": "张家界",
                "value": 48,
                "middle_data": {
                    "total_information": 221,
                    "today_information": 7,
                    "total_transaction_money": 22,
                    "today_transaction_money": 0.2,
                    "total_transaction_area": 77,
                    "today_transaction_area": 0.43
                }
            },
            {
                "name": "益阳",
                "value": 77,
                "middle_data": {
                    "total_information": 100,
                    "today_information": 3,
                    "total_transaction_money": 33,
                    "today_transaction_money": 0.3,
                    "total_transaction_area": 44,
                    "today_transaction_area": 0.7
                }
            },
            {
                "name": "郴州",
                "value": 78,
                "middle_data": {
                    "total_information": 178,
                    "today_information": 9,
                    "total_transaction_money": 28.66,
                    "today_transaction_money": 0.16,
                    "total_transaction_area": 99,
                    "today_transaction_area": 0.3
                }
            },
            {
                "name": "永州",
                "value": 57,
                "middle_data": {
                    "total_information": 222,
                    "today_information": 10,
                    "total_transaction_money": 55,
                    "today_transaction_money": 0.31,
                    "total_transaction_area": 87,
                    "today_transaction_area": 0.25
                }
            },
            {
                "name": "怀化",
                "value": 83,
                "middle_data": {
                    "total_information": 212,
                    "today_information": 6,
                    "total_transaction_money": 45,
                    "today_transaction_money": 0.31,
                    "total_transaction_area": 76.66,
                    "today_transaction_area": 0.78
                }
            },
            {
                "name": "娄底",
                "value": 43,
                "middle_data": {
                    "total_information": 122,
                    "today_information": 1,
                    "total_transaction_money": 21.1,
                    "today_transaction_money": 0.12,
                    "total_transaction_area": 65.77,
                    "today_transaction_area": 0.22
                }
            },
            {
                "name": "湘西",
                "value": 75,
                "middle_data": {
                    "total_information": 333,
                    "today_information": 22,
                    "total_transaction_money": 112,
                    "today_transaction_money": 0.91,
                    "total_transaction_area": 55.33,
                    "today_transaction_area": 0.3
                }
            }
        ]
    }
    map_data_resp = {}
    if city is None:
        map_data_resp = map_data_
    else:
        for md in map_data_.get('source'):
            if md.get('name') == city:
                map_data_resp = md.get('middle_data')
                break
    return jsonify(map_data_resp)
